package action.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biz.NewsBiz;
import biz.impl.NewsBizImpl;
import entity.News;
import entity.Users;


public class NewsEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public NewsEditServlet() {
		super();
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsBiz nb = new NewsBizImpl();
		Integer nid=Integer.parseInt(request.getParameter("nid"));
		News news= nb.getNewsById(nid);
		
		request.setAttribute("news", news);
		request.getRequestDispatcher("admin/newsEdit.jsp").forward(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsBiz nb = new NewsBizImpl();
		if(request.getParameter("nid")!=null){
			News news =new News(Integer.parseInt(request.getParameter("nid")),
					request.getParameter("newstitle"),
					request.getParameter("newscontens"),
					new Date(),
					((Users)request.getSession().getAttribute("users")).getId());
			nb.updateNews(news);
		}else{
			if(request.getParameter("newstitle")!=null &&
					request.getParameter("newscontens")!=null
					){
				News news = new News(request.getParameter("newstitle"),
						request.getParameter("newscontens"),
						new Date(),
						((Users)request.getSession().getAttribute("users")).getId());
				nb.addNews(news);

			}
		}
		response.sendRedirect("NewsListServlet");
	}

}
